package cn.zhaoyan.servlet;


import cn.zhaoyan.bean.User;
import cn.zhaoyan.service.Userservice;
import cn.zhaoyan.serviceimpl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String yanzhengma = request.getParameter("yanzhengma");

        //获取对应的Session tomcate 会自动寻找对应的Session信息
        HttpSession session = request.getSession();
        String checkcode = (String) session.getAttribute("checkcode");
        boolean equals = yanzhengma.equals(checkcode);
        if (!equals){
            request.setAttribute("us","2");
            request.getRequestDispatcher("register.jsp").forward(request, response);
            return;
        }

        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String role = request.getParameter("role");
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        user.setRole(role);
        Userservice zc = new UserServiceImpl();
        boolean b = zc.queryService(user);
        if (b){
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }else {
            request.setAttribute("ddd",user);
            request.getRequestDispatcher("register.jsp").forward(request,response);
            return;

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doPost(request,response);
    }
}
